package com.caits.lbs.framework.services.hessian.client.session;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.caucho.hessian.client.HessianProxy;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.HessianRemoteObject;

/**
 * <p>
 * SessionHessianProxyFactory
 * </p>
 * <p>
 * 自定义的代理工厂，增加了对cookies的支持，从而支持session
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2010-11-16 下午02:35:53</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public class SessionHessianProxyFactory extends HessianProxyFactory {
	
	/** 变量:TODO,类型:org.springframework.remoting.caucho.HessianProxyFactoryBean */
	org.springframework.remoting.caucho.HessianProxyFactoryBean bean;
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.caucho.hessian.client.HessianProxyFactory#create(java.lang.Class,
	 * java.lang.String, java.lang.ClassLoader)
	 */
	public Object create(Class api, String urlName, ClassLoader loader)
			throws MalformedURLException {
		if (api == null){
			throw new MalformedURLException(
					"api must not be null for HessianProxyFactory.create()");
		}
		InvocationHandler handler = null;

		URL url = new URL(urlName);
		handler = new SessionHessianProxy(url, this);

		return Proxy.newProxyInstance(loader, new Class[] { api,
				HessianRemoteObject.class }, handler);
		
		
	}
}

/**
 * <p>
 * SessionHessianProxy
 * </p>
 * <p>
 * 支持会话的代理类
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2010-11-16 下午02:36:04</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
class SessionHessianProxy extends HessianProxy {
	/** 变量:TODO,类型:long */
	private static final long serialVersionUID = -3791512971889597075L;
	/** Variable for saving cookie list */
	private List<String> cookies = null;

	public SessionHessianProxy(URL url, HessianProxyFactory factory) {
		super(url,factory);
	}

	/** Get cookie list from the headers of response */
	protected void parseResponseHeaders(URLConnection conn) {
		List<String> _cookies = conn.getHeaderFields().get("Set-Cookie");
		if (_cookies != null)
			cookies = _cookies;
	}

	/** Add cookie list to request headers */
	protected void addRequestHeaders(URLConnection conn) {
		if (cookies != null) {
			for (String cookieString : cookies) {
				conn.setRequestProperty("Cookie", cookieString);
			}
		}
	}

}
